package com.xbongbong.admin.model.impl;

import com.xbongbong.admin.domain.dao.MirrorStageDao;
import com.xbongbong.admin.domain.entity.MirrorStageEntity;
import com.xbongbong.admin.model.MirrorStageModel;
import com.xbongbong.paas.toolbox.util.CollectionsUtil;
import com.xbongbong.paas.toolbox.util.DateUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author bomfish
 */
@Service("mirrorStageModel")
public class MirrorStageModelImpl implements MirrorStageModel {
    @Resource
    private MirrorStageDao mirrorStageDao;

    @Override
    public Integer insert(MirrorStageEntity entity) {
        Long time = DateUtil.getNow();
        entity.setAddTime(time);
        entity.setUpdateTime(time);
        return mirrorStageDao.insert(entity);
    }

    @Override
    public void insertBatch(List<MirrorStageEntity> list) {
        mirrorStageDao.insertBatch(list);
    }

    @Override
    public List<MirrorStageEntity> findEntitys(Map<String, Object> param) {
        return mirrorStageDao.findEntitys(param);
    }

    @Override
    public Integer getEntitysCount(Map<String, Object> param) {
        return mirrorStageDao.getEntitysCount(param);
    }

    @Override
    public Integer update(MirrorStageEntity entity) {
        Long time = DateUtil.getNow();
        entity.setUpdateTime(time);
        return mirrorStageDao.update(entity);
    }

    @Override
    public MirrorStageEntity getByKey(Long key, String corpid) {
        return mirrorStageDao.getByKey(key, corpid);
    }

    @Override
    public List findEntitysWithoutSub(Map<String, Object> param) {
        return mirrorStageDao.findEntitys(param);
    }

    @Override
    public void updateBatch4Script(List<MirrorStageEntity> list) {
        if(CollectionsUtil.isNotEmpty(list)) {
            int size = list.size();
            int page = (size - 1) / 1000;
            for (int currPage = 0; currPage <= page; currPage++) {
                int offset = currPage * 1000;
                mirrorStageDao.updateBatch4Script(list.subList(offset, (size - offset < 1000) ? size : offset + 1000));
            }
        }
    }
}
